using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using CommonLibrary.DBUtility;
using CommonLibrary.Model;

namespace CommonLibrary.DAL
{
    public class EventLog
    {
        public bool Exsits(int LogID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@LogID",SqlDbType.Int)
            };
            parameters[0].Value = LogID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "EventLog_Exsits", parameters);
                if (dr.Read())
                    return true;
                else
                    return false;
            }
        }
        public bool Insert(EventLogInfo EventLog)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@CommPort",SqlDbType.Int),
                new SqlParameter("@Description",SqlDbType.VarChar,500),
                new SqlParameter("@HappenTime",SqlDbType.DateTime),
                new SqlParameter("@IsSuccess",SqlDbType.Bit)
            };
            parameters[0].Value = EventLog.CommPort;
            parameters[1].Value = EventLog.Description;
            parameters[2].Value = EventLog.HappenTime;
            parameters[3].Value = EventLog.IsSuccess;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "EventLog_Insert", parameters);
                return rowAffected > 0;
            }
        }
        public bool Update(EventLogInfo EventLog)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@LogID",SqlDbType.Int),
                new SqlParameter("@CommPort",SqlDbType.Int),
                new SqlParameter("@Description",SqlDbType.VarChar,500),
                new SqlParameter("@HappenTime",SqlDbType.DateTime),
                new SqlParameter("@IsSuccess",SqlDbType.Bit)
            };
            parameters[0].Value = EventLog.LogID;
            parameters[1].Value = EventLog.CommPort;
            parameters[2].Value = EventLog.Description;
            parameters[3].Value = EventLog.HappenTime;
            parameters[4].Value = EventLog.IsSuccess;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "EventLog_Update", parameters);
                return rowAffected > 0;
            }
        }
        public bool Delete(int LogID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@LogID",SqlDbType.Int)
            };
            parameters[0].Value = LogID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "EventLog_Delete", parameters);
                return rowAffected > 0;
            }
        }
        public bool DeleteAll()
        {
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "EventLog_DeleteAll");
                return rowAffected > 0;
            }
        }

        public EventLogInfo GetEventLog(int LogID)
        {
            EventLogInfo EventLog = null;
            SqlParameter[] parameters = {
                new SqlParameter("@LogID",SqlDbType.Int)
            };
            parameters[0].Value = LogID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "EventLog_GetDetail", parameters);
                if (dr.Read())
                {
                    EventLog = new EventLogInfo();
                    EventLog.LogID = dr.GetInt32(0);
                    EventLog.CommPort = dr.GetInt32(1);
                    EventLog.Description = dr.GetString(2);
                    EventLog.HappenTime = dr.GetDateTime(3);
                    EventLog.IsSuccess = dr.GetBoolean(4);
                }
                return EventLog;
            }
        }
        public List<EventLogInfo> GetList(string StrWhere)
        {
            List<EventLogInfo> EventLogs = new List<EventLogInfo>();
            EventLogInfo EventLog = null;
            SqlParameter[] parameters = {
                new SqlParameter("@StrWhere",SqlDbType.NVarChar,4000)
            };
            parameters[0].Value = StrWhere;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "EventLog_GetList", parameters);
                while (dr.Read())
                {
                    EventLog = new EventLogInfo();
                    EventLog.LogID = dr.GetInt32(0);
                    EventLog.CommPort = dr.GetInt32(1);
                    EventLog.Description = dr.GetString(2);
                    EventLog.HappenTime = dr.GetDateTime(3);
                    EventLog.IsSuccess = dr.GetBoolean(4);
                    EventLogs.Add(EventLog);
                }
                return EventLogs;
            }
        }

    }
}